Cellen 2

R basics I — R & RStudio

Gavin Simpson

Aarhus University

2025-02-12

Learning objectives

At the end of this topic you should be able to

  • Understand the main features of the RStudio IDE

  • Run simple R commands in RStudio

  • Understand the basic syntax of R

  • Understand how to use the R help system

R

R is a powerful software application for statistical analysis

It is incredibly popular

  • It is open source — GPL
  • Vast package ecosystem
  • Designed from the ground up for analysing data
  • Has excellent graphics capabilities

R is an interpreted language unlike C, C++, etc

Slower but more forgiving and interactive

RStudio

RStudio is a powerful integrated development environment (IDE) for R

  • an interface for running R
  • an editor for writing R scripts
  • menus & buttons to run common tasks
  • a lot more

It is also open source

RStudio ≠ R

Can run RStudio on your computer or in the cloud using posit.cloud

RStudio PBC provide paid-for support & Pro-level versions for organisations

RStudio

R example

# Palmer penguins
# Load some packages
library("palmerpenguins")
library("dplyr")
library("ggplot2")

# how many observations of each species of penguin?
penguins |>
    count(species)
## # A tibble: 3 × 2
##   species       n
##   <fct>     <int>
## 1 Adelie      152
## 2 Chinstrap    68
## 3 Gentoo      124

R example

penguins |> 
  group_by(species) |> 
    summarize(across(where(is.numeric), mean, na.rm = TRUE))
## # A tibble: 3 × 6
##   species   bill_length_mm bill_depth_mm flipper_length_mm body_mass_g  year
##   <fct>              <dbl>         <dbl>             <dbl>       <dbl> <dbl>
## 1 Adelie              38.8          18.3              190.       3701. 2008.
## 2 Chinstrap           48.8          18.4              196.       3733. 2008.
## 3 Gentoo              47.5          15.0              217.       5076. 2008.

R example

ggplot(penguins, aes(x = flipper_length_mm,
                     y = body_mass_g,
                     colour = species,
                     shape  = species)) +
  geom_point(size = 3) +
  scale_colour_brewer(palette = "Set1")

R example

Don’t worry! You won’t understand most of that!

By the end of the course you will

R basics

Assignment

<- is the assignment operator

Made up from the < and - characters

output <- input

Assign the result of the right hand side to the object named on the left

This creates an object with name output

Refer to objects using their name

Data types

The main data types in R are

  • numeric
    • integer
    • double (real values)
    • complex (numbers with real & imaginary parts)
  • character
    • strings of letters, numbers, etc
    • create with matched single ' or double " quotes
  • logical
    • TRUE and FALSE

Never use T and F in their place!

TRUE & FALSE are reserved words in R — can’t be overwritten — but T and F aren’t

T <- FALSE # you monster!
T == TRUE
## [1] FALSE

Operators

As well as <- R has many operators

  • Mathematical

    • +
    • -
    • *
    • /
  • Boolean

    • < and >
    • <= and >= (< = & > =)
    • == (= =)
    • != (! =)
    • & AND
    • | OR
    • ! NOT

Getting help

Can get help on R from many places

Inside R use ?topic to get help on topic topic

Usually topic is a function

Can search more broadly with ??topic

Other sources: